// 
// DotNetNuke?- http://www.dotnetnuke.com 
// Copyright (c) 2002-2011 
// by DotNetNuke Corporation 
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions: 
// 
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software. 
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE. 
// 

using System;
using System.Configuration;
using System.Data;
using System.Xml;
using System.Web;
using System.Collections.Generic;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Services.Search;
using DotNetNuke.Entities.Modules;

namespace CharityGamesOnline.Modules.Charity
{

    /// ----------------------------------------------------------------------------- 
    /// <summary> 
    /// The Controller class for Charity 
    /// </summary> 
    /// <remarks> 
    /// </remarks> 
    /// <history> 
    /// </history> 
    /// ----------------------------------------------------------------------------- 
    public class CharityController : ISearchable, IPortable
    {

        #region "Public Methods"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// gets an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleId">The Id of the module</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public List<CharityInfo> GetCharities(int PortalID)
        {
            return GetCharities(PortalID, 0);
        }

        public List<CharityInfo> GetCharities(int PortalID, int EventID)
        {
            return CBO.FillCollection<CharityInfo>(DataProvider.Instance().GetCharities(PortalID, EventID));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PortalID"></param>
        /// <param name="CharityID"></param>
        /// <returns></returns>
        public CharityInfo GetCharity(int PortalID, int CharityID)
        {

            return (CharityInfo)CBO.FillObject(DataProvider.Instance().GetCharity(PortalID, CharityID), typeof(CharityInfo));

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// adds an object to the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="objCharity">The CharityInfo object</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void SaveCharity(CharityInfo objCharity)
        {            
            if (objCharity.Name.Trim() != string.Empty)
            {
                DataProvider.Instance().SaveCharity(
                    objCharity.CharityID, objCharity.PortalID, objCharity.LastUpdatedID, objCharity.Status, objCharity.Name,
                    objCharity.Description, objCharity.Contact, objCharity.WebSite, objCharity.Email, objCharity.Telephone,
                    objCharity.Fax, objCharity.Address, objCharity.Category, objCharity.ImageURL, objCharity.ImageType,
                    objCharity.ImageWidth, objCharity.ImageHeight, objCharity.ImageDisplay);
            }
        }

        
        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// deletes an object from the database 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="PortalID">The Id of the module</param> 
        /// <param name="CharityID">The Id of the item</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void DeleteCharity(int PortalID, int CharityID)
        {

            DataProvider.Instance().DeleteCharity(PortalID, CharityID);

        }

        #endregion

        #region "Optional Interfaces"

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// GetSearchItems implements the ISearchable Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModInfo">The ModuleInfo for the module to be Indexed</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public DotNetNuke.Services.Search.SearchItemInfoCollection GetSearchItems(ModuleInfo ModInfo)
        {

            //SearchItemInfoCollection SearchItemCollection = new SearchItemInfoCollection();

            //List<CharityInfo> colCharitys = GetCharitys(ModInfo.ModuleID);
            //foreach (CharityInfo objCharity in colCharitys)
            //{
            //    SearchItemInfo SearchItem = new SearchItemInfo(ModInfo.ModuleTitle, objCharity.Content, objCharity.CreatedByUser, objCharity.CreatedDate, ModInfo.ModuleID, objCharity.ItemId.ToString(), objCharity.Content, "ItemId=" + objCharity.ItemId.ToString());
            //    SearchItemCollection.Add(SearchItem);
            //}

            //return SearchItemCollection;
            return null;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ExportModule implements the IPortable ExportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be exported</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public string ExportModule(int PortalID)
        {

            string strXML = "";

            List<CharityInfo> lstCharity = GetCharities(PortalID);
            if (lstCharity.Count != 0)
            {
                strXML += "<Charities>";
                foreach (CharityInfo objCharity in lstCharity)
                {
                    strXML += "<Charity>";
                    strXML += "<content>" + XmlUtils.XMLEncode(objCharity.Name) + "</content>";
                    strXML += "</Charity>";
                }
                strXML += "</Charities>";
            }

            return strXML;

        }

        /// ----------------------------------------------------------------------------- 
        /// <summary> 
        /// ImportModule implements the IPortable ImportModule Interface 
        /// </summary> 
        /// <remarks> 
        /// </remarks> 
        /// <param name="ModuleID">The Id of the module to be imported</param> 
        /// <param name="Content">The content to be imported</param> 
        /// <param name="Version">The version of the module to be imported</param> 
        /// <param name="UserId">The Id of the user performing the import</param> 
        /// <history> 
        /// </history> 
        /// ----------------------------------------------------------------------------- 
        public void ImportModule(int ModuleID, string Content, string Version, int UserId)
        {

            //XmlNode xmlCharitys = Globals.GetContent(Content, "Charitys");
            //foreach (XmlNode xmlCharity in xmlCharitys.SelectNodes("Charity"))
            //{
            //    CharityInfo objCharity = new CharityInfo();
            //    objCharity.ModuleId = ModuleID;
            //    objCharity.Content = xmlCharity.SelectSingleNode("content").InnerText;
            //    objCharity.CreatedByUser = UserId;
            //    AddCharity(objCharity);
            //}
        }

        #endregion

    }
}